﻿namespace UnitFundProfitability
{
	using System;
	using System.Windows;
	using System.Windows.Controls;
	using UnitFundProfitability.BusinessLogic;

	/// <summary>
	/// Interaction logic for MainWindow.xaml.
	/// </summary>
	public partial class MainWindow : Window
	{
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="MainWindow"/> class.
		/// </summary>
		public MainWindow()
		{
			InitializeComponent();
			UpdatesWindow splashScreen = new UpdatesWindow();
			UpdatesManager.Updating += (sender, eventArgs) => splashScreen.Show();
			UpdatesManager.Updated += (sender, eventArgs) => splashScreen.Close();
			UpdatesManager.EnsureLatestData();
			StartDate.SelectedDate = DateTime.Today.AddMonths(-1);
			EndDate.SelectedDate = DateTime.Today;
			RefreshFundsProfitabilities();
			StartDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(StartDate_SelectedDateChanged);
			EndDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(EndDate_SelectedDateChanged);
			AddManagementCompany.Click += new RoutedEventHandler(AddManagementCompany_Click);
			AddUnitFund.Click += new RoutedEventHandler(AddUnitFund_Click);
			SetMarkupRate.Click += new RoutedEventHandler(SetMarkupRate_Click);
			SetMarkdownRate.Click += new RoutedEventHandler(SetMarkdownRate_Click);
			SetPrice.Click += new RoutedEventHandler(SetPrice_Click);
			BuyUnits.Click += new RoutedEventHandler(BuyUnits_Click);
			SellUnits.Click += new RoutedEventHandler(SellUnits_Click);
			MoveUnits.Click += new RoutedEventHandler(MoveUnits_Click);
		}
		#endregion

		#region Methods
		/// <summary>
		/// Refreshes the funds profitabilities.
		/// </summary>
		private void RefreshFundsProfitabilities()
		{
			FundsProfitabilities.DataContext = Manager.CalculateFundsProfitabilities(StartDate.SelectedDate.Value, EndDate.SelectedDate.Value);
		}

		/// <summary>
		/// Handles the SelectedDateChanged event of the StartDate control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
		private void StartDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the SelectedDateChanged event of the EndDate control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
		private void EndDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the AddManagementCompany control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void AddManagementCompany_Click(object sender, RoutedEventArgs e)
		{
			ManagementCompanies dialog = new ManagementCompanies();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the AddUnitFund control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void AddUnitFund_Click(object sender, RoutedEventArgs e)
		{
			UnitFunds dialog = new UnitFunds();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the SetMarkupRate control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void SetMarkupRate_Click(object sender, RoutedEventArgs e)
		{
			MarkupRates dialog = new MarkupRates();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the SetMarkdownRate control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void SetMarkdownRate_Click(object sender, RoutedEventArgs e)
		{
			MarkdownRates dialog = new MarkdownRates();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the SetPrice control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void SetPrice_Click(object sender, RoutedEventArgs e)
		{
			Prices dialog = new Prices();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the BuyUnits control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void BuyUnits_Click(object sender, RoutedEventArgs e)
		{
			Incomes dialog = new Incomes();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the SellUnits control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void SellUnits_Click(object sender, RoutedEventArgs e)
		{
			Expenditures dialog = new Expenditures();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}

		/// <summary>
		/// Handles the Click event of the MoveUnits control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void MoveUnits_Click(object sender, RoutedEventArgs e)
		{
			Movements dialog = new Movements();
			dialog.ShowDialog();
			RefreshFundsProfitabilities();
		}
		#endregion
	}
}
